package org.manusoft.refill.view;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.manusoft.refill.domain.TVehicleDetailsRf;
import org.manusoft.refill.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("editVehicle")
@Scope("view")
public class EditVehicle implements Serializable {

    private TVehicleDetailsRf vehicleDetailsRf;
    private VehicleService vehicleService;
    private Integer vehicleId;

    public EditVehicle() {
    }

    @Autowired
    public EditVehicle(VehicleService vehicleService) {
        this.vehicleService = vehicleService;
    }

    public void onLoad() {
    	vehicleDetailsRf = vehicleService.findById(vehicleId,false);
    }

    public TVehicleDetailsRf getVehcile(){
        return vehicleDetailsRf;
    }

    /**
	 * @return the vehicleDetailsRf
	 */
	public TVehicleDetailsRf getVehicle() {
		return vehicleDetailsRf;
	}

	/**
	 * @param vehicleDetailsRf the vehicleDetailsRf to set
	 */
	public void setVehicle(TVehicleDetailsRf vehicleDetailsRf) {
		this.vehicleDetailsRf = vehicleDetailsRf;
	}

	/**
	 * @return the vehicleId
	 */
	public Integer getVehicleId() {
		return vehicleId;
	}

	/**
	 * @param vehicleId the vehicleId to set
	 */
	public void setVehicleId(Integer vehicleId) {
		this.vehicleId = vehicleId;
	}

	public void update() {
        vehicleService.createUpdateVehicle(getVehcile());
        FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Vehicle details are updated successfully", "OK");
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
    }

    public String remove() {
    	vehicleService.delete(vehicleDetailsRf);
        return "manageVehicles?faces-redirect=true";
    }
}
